<template>
    <view class="logining">登录中...</view>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref, onUnmounted } from 'vue'
import Button from '@/components/Button.vue'
import Api from '@/apis/api'
import { isPhone } from '@/utils/utils'
import { showToast, showLoading } from '@/utils/prompt'
import { useUserStore } from '@/store/user'
import {useRouter,useRoute} from 'vue-router'
let timer: any = null
const userStore = useUserStore();

const loginData = reactive({
	username: '',
	code: '',
	wxCode:''
})
let code='';
let state=''
let loginType=0

onMounted(() => {
	if (userStore.isLogin) {
		uni.reLaunch({
			url: '/pages/home/index'
		});
	}
	const route=useRoute()
	code=getUrlCode().code
	state = getUrlCode().state
	//loginType = getUrlCode().loginType
	//console.log("getUrlCode().loginType ======",getUrlCode().loginType)
	//alert("loginType==="+loginType)
	if(code){
		login()
	}
})

onUnmounted(() => {
	clearInterval(timer)
})



function IsPC(){
	var userAgentInfo = navigator.userAgent;
	var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
	var flag = true;
	for (var v = 0; v < Agents.length; v++) {
		if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }
	}
	return flag;
}

function  toweappLogin() {
	//alert("code =="+code)
	loginType=1
	//alert("loginType=="+loginType)
	checkWeChatCode()
}


function checkWeChatCode() {
	if(loginType==1){
		//alert(6)
		// 页面一进来先判断是否有code
		code = getUrlCode().code;
		if (code!='' && code !=null && code!=undefined) {
			//alert(7)
			// 如果有，走登记接口
			login()
		} else {
			getWxCode()
			// 根据需求使用 this.getCode()
		}
	}
}


// 获取微信用户code
function  getWxCode() {
	console.log("location.href===",location.href)

	const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx764469496c53af1b&redirect_uri=${encodeURIComponent(location.href
	)}&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect`;
	//如果没有code 去获取code
	window.location.href = url;

}



// 截取url中的code
function  getUrlCode() {
	var url = location.search;
	var theRequest = new Object();
	if (url.indexOf("?") !== -1) {
		var str = url.substr(1);
		var strs = str.split("&");
		for (var i = 0; i < strs.length; i++) {
			theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
		}
	}
	return theRequest;
}

async function login() {
	console.log("开始登录");
	loginData.wxCode=code
	Api.mplogin(loginData).then((data: any) => {
		console.log("登录成功后的返回参数== ",data)
		if(data.code ==200){
			uni.reLaunch({
				url: '/pages/home/index'
			});
			showToast('登陆成功')
			userStore.setToken(data.token, true)
			code=''
		}else{
			showToast('登陆失败，请重新登录')
		}

	}).finally(() => {
		uni.hideLoading()
	})
}

</script>

<style lang="scss" scoped>
.page-login {
	font-family: PingFangSC-Regular-Regular;

	.logo {
		padding-top: 140rpx;

		.icon {
			display: block;
			margin: auto;
		}

		.icon-1 {
			width: 128rpx;
			height: 128rpx;
		}

		.icon-2 {
			width: 180rpx;
			height: 40rpx;
			margin-top: 24rpx;
		}
	}

	.login-type {
		color: #333;
		position: relative;
		margin-top: 85rpx;
		text-align: center;

		.active {
			font-size: 32rpx;
			color: #333;
			font-family: PingFang SC Bold;
			position: relative;

			&::after {
				content: '';
				display: inline-block;
				position: absolute;
				width: 50rpx;
				height: 8rpx;
				background-color: #FF544F;
				border-radius: 0.15625rem;
				left: 50%;
				bottom: -15rpx;
				transform: translate(-50%);
			}
		}

		.default {
			font-size: 32rpx;
			color: #999999;
			margin-left: 100rpx;
		}
	}

	.form-wrapper {
		margin-top: 66rpx;
		padding: 0 80rpx;
		font-size: 28rpx;

		.btn {
			.tips {
				color: #999999;
				display: inline-block;
				font-size: 28rpx;
				margin-top: 20rpx;
			}
		}

		.xy {
			color: #9E9E9E;
			margin-top: 20rpx;
			font-size: 24rpx;

			.up {
				color: #FF544F;
			}
		}

		.uni-input {
			border: 1px solid #E2E2E2;
			border-radius: 12rpx;
			height: 80rpx;
			text-indent: 28rpx;

		}

		.code-input {
			position: relative;
			margin-top: 30rpx;

			.code-text {
				font-size: 28rpx;
				color: #cbcbcb;
				position: absolute;
				right: 28rpx;
				top: 50%;
				transform: translate(0, -50%);

				&::before {
					content: '';
					display: inline-block;
					background: #E8E8E8;
					width: 2rpx;
					height: 36rpx;
					position: absolute;
					left: -20rpx;
				}
			}
		}
	}

}
    .logining{
        margin-top: 50rpx;
        text-align: center;
    }
</style>